// pages/order/orderdetails/orderdetails.js
const app = getApp()
var util = require('../../../utils/util.js')
var http = require('../../../utils/server.js')
Page({
  data: {
    data: null,
    btn: null,
    showAuthor: false,
    id: null,
    showGangMasterTip: false,
    showGangTip: false,
    showFansTip: false,
    express: [],
    expressIndex: -1,
    expressNumber: ''
  },
  onLoad: function(options) {
    var that = this;
    that.setData({
      id: options.id
    })

    //用户信息
    app.getUserInfo(function(userInfo) {
      if (userInfo != null) {
        console.info('用户信息', userInfo)
        that.setData({
          userInfo: userInfo,
          showAuthor: false
        })
      } else {
        that.setData({
          showAuthor: true
        })
      }
    })

  },
  onShow: function() {
    this.getData()

  },
  getData: function() {
    var that = this;
    http.post({
      url: "Order/getInfo",
      order_id: that.data.id,
      // status: status,
    }, function(data) {
      that.setData({
        data: data,
        expressNumber: data.buyer_send_number || ''
      });
      if (data.status == 13 || data.status == 4) {
        http.post({
          url: "Address/getAllExpress",
          type: ''
        }, function(res) {
          console.log('所有快递', res)
          var expressIndex = -1
          if (data.status == 4) {
            for (var i in res) {
              if (res[i].code == data.buyer_send_code) {
                expressIndex = i
              }
            }

          }
          that.setData({
            express: res,
            expressIndex: expressIndex
          });

        })
      }
    });
  },
  onParentEvent: function(e) {
    var that = this
    app.loginServer(app.globalData.code, e.detail.encryptedData, e.detail.iv, that.getData)
  },
  bindPickerChange: function(e) {
    console.log(e.detail.value)
    var that = this
    that.setData({
      expressIndex: e.detail.value
    })
  },
  applyRefund: function(e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/order/applyrefund/applyrefund?orderId=' + that.data.id,
    })
  },
  fahuo: function(e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/order/delivery/delivery?id=' + that.data.id,
    })
  },
  preview: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var urls = that.data.data.apply_imgs
    wx.previewImage({
      current: urls[index],
      urls: urls
    })
  },
  wuliu: function() {
    wx.navigateTo({
      url: '/pages/order/logis/logistical',
    })
  },
  logistical: function(e) {
    var number = e.currentTarget.dataset.number;
    var code = e.currentTarget.dataset.code;
    wx.navigateTo({
      url: '/pages/order/logis/oneLogistical?code=' + code + '&&number=' + number,
    })
  },
  receive: function(e) {
    var that = this; 
    wx.showModal({
      title: '操作提示',
      content: '确定收货吗?',
      success: function(res) {
        if (res.confirm) {
          http.post({
            url: "Order/confirmReceipt",
            order_id: that.data.id
          }, function(data) {
            util.showSuccess("操作成功");
          });
        }
      }
    })
  },
  evaluate: function(e) {
    var that = this;
    var order_id = e.currentTarget.dataset.id
    var product_id = e.currentTarget.dataset.pid
    wx.navigateTo({
      url: '/pages/evaluate/evaluate/evaluate?order_id=' + order_id + "&product_id=" + product_id
    })
  },
  gohome: function() {
    wx.reLaunch({
      url: '/pages/index/welcome'
    })
  },
  copy: function(e) {
    var data = e.currentTarget.dataset.content
    wx.setClipboardData({
      data: data,
      success: function (res) {
        util.showSuccess('复制成功')
      }
    })
  },
  pay: function(e) {
    var that = this
    http.post({
      url: "order/pay",
      order_id: that.data.id,
    }, function(data) {
      wx.requestPayment({
        'timeStamp': data.timeStamp,
        'nonceStr': data.nonceStr,
        'package': data.package,
        'signType': data.signType,
        'paySign': data.paySign,
        'success': function(e) {
          console.log("支付成功！", e)
          util.showSuccess('支付成功！', function() {
            wx.redirectTo({
              url: "/pages/order/paySuccess/paySuccess?order_id=" + data.order_id + '&money=' + data.recharge
            })
          })
        },
        'fail': function(e) {
          console.log("支付失败！", e)
          util.showError('支付失败！')
        }
      })
    })
  },
  bindExpressNumber: function(e) {
    this.setData({
      expressNumber: e.detail.value
    })
  },
  showRedpacketTip: function(e) {
    var that = this
    var type = e.currentTarget.dataset.type
    if (type == "帮主包") {
      that.setData({
        showGangMasterTip: !that.data.showGangMasterTip
      })
    } else if (type == "帮会包") {
      that.setData({
        showGangTip: !that.data.showGangTip
      })

    } else if (type == "粉丝包") {
      that.setData({
        showFansTip: !that.data.showFansTip
      })

    }
  },
  returnGoods: function() {
    var that = this;
    var code = that.data.express[that.data.expressIndex] || ''
    var number = that.data.expressNumber
    if (!code) {
      util.showError('请选择物流公司')
      return
    }
    if (!number) {
      util.showError('请填写运单号')
      return
    }
    code = code.code
    http.post({
      url: "order/buyerSendGoods",
      order_id: that.data.id,
      code: code,
      number: number
    }, function(data) {
      console.log('买家发货返回', data)
      util.showSuccess('发货成功', function() {
        wx.navigateBack({
          delta: 1
        })
      })

    })
  }

})